package com.java01.file.bufferedinputstream;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class PipeReader {

	public PipeReader() {
		File pipeFile = new File("pipe");
		pipeFile.deleteOnExit();
		if (!pipeFile.exists()) {
			try {
				Runtime.getRuntime().exec("mkfifo " + pipeFile.getAbsolutePath()).waitFor();
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}
		}

		BufferedInputStream bis;

		try {
			bis = new BufferedInputStream(new FileInputStream(pipeFile));
			while (true) {
				byte[] buffer = new byte[4096];
				if (bis.available() > 0) {
					bis.read(buffer, 0, bis.available());
					System.out.print(new String(buffer));
				} else {
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new PipeReader();
	}
}
